package qeb.handheldmedical.patient;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Picture;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.TextView;
import qeb.handheldmedical.R;
import qeb.handheldmedical.adapter.SlideListView;
import qeb.handheldmedical.adapter.TemperatureAdapter;
import qeb.handheldmedical.dialog.MultiDialog;
import qeb.handheldmedical.helper.WSHelper;
import qeb.handheldmedical.model.InPatient;
import qeb.handheldmedical.model.User;

public class Temperature extends Activity {
	private SlideListView slideListView;
	private TemperatureAdapter adapter;
	InPatient ip = new InPatient();
	private JSONArray twJsonArray;
	private User usr = new User();
	private Button btn_title_back;
	private Button btn_title_right;
	private TextView tv_title;
	private MultiDialog mDialog;
	private static final int UPDATE = 0;
	private JSONObject twData;
	private qeb.handheldmedical.model.Temperature temperature;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		ip = (InPatient) getIntent().getSerializableExtra("patientinfo");
		usr = (User) getIntent().getSerializableExtra("userinfo");
		setContentView(R.layout.query_temperature);
		initView();
		initData();
	}

	private void initData() {
		mDialog = new MultiDialog(Temperature.this,
				MultiDialog.DialogType.WAITING);
		mDialog.setMessage("正在加载，请稍候......");
		mDialog.show();
		new Thread() {

			@Override
			public void run() {
				try {
					getData();
					myHandler.sendEmptyMessage(UPDATE);
				} catch (Exception e) {
					// TODO: handle exception
					// Log.v("LineExam", e.toString());
				}
			}
		}.start();

	}

	Handler myHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case UPDATE:
				dataBind();
				break;
			default:
				break;
			}
			super.handleMessage(msg);
			// isRunning=false;
			mDialog.cancel();
		}
	};

	private void initView() {
		slideListView = (SlideListView) findViewById(R.id.slideListView4);
		tv_title = (TextView) findViewById(R.id.tv_top_title);
		tv_title.setText(ip.XM + "的体温记录");
		btn_title_back = (Button) findViewById(R.id.btn_title_back);
		btn_title_back.setOnClickListener(MyOnClickListener);

		btn_title_right = (Button) findViewById(R.id.btn_title_right);
		btn_title_right.setText("新增");
		btn_title_right.setOnClickListener(MyOnClickListener);

	}

	/* 系统主菜单点击事件（在院病人、门诊病人等） */
	private OnClickListener MyOnClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO 自动生成的方法存根
			// Bundle bundle=new Bundle();
			switch (v.getId()) {
			case R.id.btn_title_back:// 返回按钮
				Temperature.this.finish();
				break;
			case R.id.btn_title_right:// 添加
				Intent intent = new Intent(Temperature.this,
						qeb.handheldmedical.patient.EditTemperature.class);
				intent.putExtra("patientinfo", ip);
				intent.putExtra("userinfo", usr);
				Temperature.this.startActivityForResult(intent, 1);
				//Temperature.this.getApplicationContext().startActivity(intent);
				break;
			default:
				break;
			}
		}
	};

	private void getData() {

		twJsonArray = WSHelper.GetTemperature(ip.ZYXH);
	}

	// 接收新的Activiity 返回的结果
	/**
	 * requestCode:10 resultCode:100
	 * */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (resultCode == RESULT_OK) {

			String isUpdate = data.getExtras().getString("UPDATE");
			if (isUpdate.equals("1")) {
				initData();
			}
		}
		super.onActivityResult(requestCode, resultCode, data);

	}

	private void dataBind() {

		if (twJsonArray != null && twJsonArray.length() > 0) {
			adapter = new TemperatureAdapter(twJsonArray.length(),
					Temperature.this, twJsonArray);
			slideListView.setAdapter(adapter);

			slideListView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> tw, View view,
						int position, long id) {
					// TODO Auto-generated method stub
					temperature = new qeb.handheldmedical.model.Temperature();
					try {
						twData = twJsonArray.getJSONObject(position);
						temperature.getValue(twData);
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (InstantiationException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					Intent intent = new Intent(Temperature.this,
							qeb.handheldmedical.patient.EditTemperature.class);
					intent.putExtra("patientinfo", ip);
					intent.putExtra("userinfo", usr);
					intent.putExtra("temperature", temperature);
					Temperature.this.startActivityForResult(intent, 1);
					// Temperature.this.getApplicationContext().startActivity(
					// intent);

				}
			});

		}
	}
}